package com.duleme.dao;

import com.duleme.common.DBUtils;
import com.duleme.entity.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDao {
    public Boolean insertAdmin(Admin admin){
        String sql = "INSERT INTO admin (AdminName,AdminPassword,CreateTime) VALUES(?,?,?)";
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,admin.getAdminName());
            preparedStatement.setString(2,admin.getAdminPwd());
            preparedStatement.setDate(3,new java.sql.Date(new Date().getTime()));
            if (preparedStatement.executeUpdate()>0){
                DBUtils.close(connection,preparedStatement,null);
                return true;
            }

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return  false;
    }

    public Admin findAdminByID(int userID, String password){
        String sql = "SELECT * FROM admin WHERE AdminID = ? AND AdminPassword = ?;";
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1,userID);
            statement.setString(2,password);
            ResultSet rs = statement.executeQuery();
            while (rs.next()){
                Admin admin = new Admin();
                admin.setAdminID(rs.getInt(1));
                admin.setAdminName(rs.getString(2));
                //admin.setAdminPwd(rs.getString(3));
                admin.setCreateTime(new Date(rs.getDate(4).getTime()));
                return admin;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<Admin> getAdminByName(String adminName){
        String sql = "SELECT * FROM admin WHERE AdminName = ? ; ";
        List<Admin> list = new ArrayList<Admin>();
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1,adminName);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                Admin admin = new Admin();
                admin.setAdminID(resultSet.getInt(1));
                admin.setAdminName(resultSet.getString(2));
                admin.setAdminPwd(resultSet.getString(3));
                admin.setCreateTime(new Date(resultSet.getDate(4).getTime()));
                list.add(admin);
            }
            DBUtils.close(connection,statement,resultSet);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return list;
    }

    public List<Admin> getAdminAll(){
        String sql = "select * from admin;";
        List<Admin> list = new ArrayList<Admin>();
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            while (rs.next()){
                Admin admin = new Admin();
                admin.setAdminID(rs.getInt(1));
                admin.setAdminName(rs.getString(2));
                admin.setAdminPwd(rs.getString(3));
                admin.setCreateTime(new Date(rs.getDate(4).getTime()));
                list.add(admin);
            }
            DBUtils.close(connection,statement,rs);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  list;
    }

    public Boolean delete(Integer[] ids){
        String sql = "DELETE FROM admin WHERE AdminID IN (";
        try {
            for (Integer id:ids) {
                sql+=id+",";
            }
            sql = sql.substring(0,sql.length()-1)+")";
            Connection connection = DBUtils.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            if (statement.executeUpdate()>0){
                return true;
            }
            DBUtils.close(connection,statement,null);

        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

}
